# (1) 定义一个Entry类
class Entry:
    def __int__(self):
        self.no = 0
        self.text = ''


class Menu:
    def __init__(self):
        # print('构造函数')
        # (2) 生成菜单数据
        e1 = Entry()
        e1.no = 1
        e1.text = '增加一个学生信息'

        e2 = Entry()
        e2.no = 2
        e2.text = '删除一个学生信息'

        e3 = Entry()
        e3.no = 3
        e3.text = '修改一个学生信息'

        e4 = Entry()
        e4.no = 4
        e4.text = '查询一个学生信息'

        e5 = Entry()
        e5.no = 5
        e5.text = '查询所有学生信息'

        e6 = Entry()
        e6.no = 6
        e6.text = '排序'

        e7 = Entry()
        e7.no = 0
        e7.text = '退出'

        self.e = []  # 成员变量
        self.e.append(e1)
        self.e.append(e2)
        self.e.append(e3)
        self.e.append(e4)
        self.e.append(e5)
        self.e.append(e6)
        self.e.append(e7)

    def print_menu(self):
        # print('打印菜单') ctrl + /
        # (3) 打印菜单
        print('****菜单开始****')
        for item in self.e:
            print(item.no, item.text)
        print('****菜单结束****')


class StudentInfoSystem:

    def __init__(self):
        # 类的成员变量
        self.students = []

        # 从txt文件中读取数据并放到学生列表中
        with open('data.txt', 'r', encoding='utf-8') as f:
            lines = f.readlines()

            for item in lines:
                tokens = item.split(' ')
                tokens[2] = tokens[2].replace('\n','')
                self.students.append(tokens)



        # 打印所有学生的信息
        # s1 = ['张三', 10, '女']
        # s2 = ['李四', 16, '男']
        #
        # self.students.append(s1)
        # self.students.append(s2)

    # 处理选项1的函数
    def save(self):
        print('增加一个学生')

        s = []
        s.append(input('姓名:'))
        s.append(input('年龄:'))
        s.append(input('性别:'))

        # 为了确保这个学生信息存储在变量s中
        print(s)

        # 将新增加的学生信息添加到学生列表
        self.students.append(s)

    def remove(self):
        print('删除')

        # 健壮性不好
        n = input('请输入待删除的学生下标：')
        self.students.pop(int(n))

    def update(self):
        print('更新')

    def find_by_name(self):
        print('根据姓名查询')

    def find_all(self):
        print('查询所有')

        for item in self.students:
            print('姓名:', item[0],
                  '年龄:', item[1],
                  '性别:', item[2])

    def sort(self):
        print('排序')

    def execute(self):
        print('系统正在运行...')

        while True:
            menu = Menu()
            menu.print_menu()  # ctrl + b

            n = input('请选择菜单: ')
            if n == '0':
                with open('data.txt', 'w', encoding='utf-8') as f:

                    for item in self.students:
                        f.write(f'{item[0]} {item[1]} {item[2]}\n')

                break
            elif n == '1':
                # print('1')
                self.save()

            elif n == '2':
                # print('2')
                self.remove()

            elif n == '3':
                # print('3')
                self.update()

            elif n == '4':
                # print('4')
                self.find_by_name()

            elif n == '5':
                # print('5')
                self.find_all()

            elif n == '6':
                # print('6')
                self.sort()

            else:
                print('无效菜单!')


system = StudentInfoSystem()
system.execute()
